/***********************************************************************************
 * 文件名： mcu_general_timer.c
 * 作者： 刘言
 * 版本： 1
 * 说明：
 * 		通用定时器驱动。
 * 修改记录：
 * 	2024/9/21: 初版。 刘言。
***********************************************************************************/
#include "mcu_general_timer.h"

#define _TIM14_ARPE      0   // 0： TIM14_ARR 寄存器没有缓冲。1： TIM14_ARR 寄存器被装入缓冲器


#ifdef _GENTIME_SINGLETON_PATTERN
void GenTim_Init()
{
    TIM14->CR1 = (_TIM14_ARPE << 7);
    TIM14->CCMR1 = (_TIM14_CC1S << 0) |
#if (_TIM14_CC1S == 0)
                (_TIM14_OC1PE << 3) |
                (_TIM14_OC1M << 4) ;
#else
                (_TIM14_IC1PSC << 2) |
                (_TIM14_IC1F << 4) ;
#endif
    TIM14->CCER = (_TIM14_CC1E << 0) |
                (_TIM14_CC1P << 1) |
                (_TIM14_CC1NP << 3) ;
    TIM14->PSC = _TIM14_PSC;
    TIM14->ARR = _TIM14_ARR;
    /* Generate an update event to reload the Prescaler
     and the repetition counter value (if applicable) immediately */
    SET_BIT(TIM14->EGR, TIM_EGR_UG);
    TIM14->SR = 0;
    TIM14->DIER = (_TIM14_UIE << 0) |
                (_TIM14_CC1IE << 1) ;
    TIM14->CCR1 = _TIM14_CCR1;
#if _TIM14_RUN == 1
    SET_BIT(TIM14->CR1, TIM_CR1_CEN);
#endif
}
#else
void GenTim_Init()
{
    TIM14->CR1 = (_TIM14_ARPE << 7);
    TIM14->CCMR1 = (_TIM14_CC1S << 0) |
#if (_TIM14_CC1S == 0)
                (_TIM14_OC1PE << 3) |
                (_TIM14_OC1M << 4) ;
#else
                (_TIM14_IC1PSC << 2) |
                (_TIM14_IC1F << 4) ;
#endif
    TIM14->CCER = (_TIM14_CC1E << 0) |
                (_TIM14_CC1P << 1) |
                (_TIM14_CC1NP << 3) ;
    TIM14->PSC = _TIM14_PSC;
    TIM14->ARR = _TIM14_ARR;
    /* Generate an update event to reload the Prescaler
     and the repetition counter value (if applicable) immediately */
    SET_BIT(TIM14->EGR, TIM_EGR_UG);
    TIM14->SR = 0;
    TIM14->DIER = (_TIM14_UIE << 0) |
                (_TIM14_CC1IE << 1) ;
    TIM14->CCR1 = _TIM14_CCR1;
#if _TIM14_RUN == 1
    SET_BIT(TIM14->CR1, TIM_CR1_CEN);
#endif
}
#endif


